<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!-- Copyright 1997 The Open Group, All Rights Reserved -->
<title>xargs</title>
</head><body bgcolor=white>
<center>
<font size=2>
The Single UNIX &reg; Specification, Version 2<br>
Copyright &copy; 1997 The Open Group

</font></center><hr size=2 noshade>
<h4><a name = "tag_001_014_2923">&nbsp;</a>NAME</h4><blockquote>
xargs - construct argument lists and invoke utility
</blockquote><h4><a name = "tag_001_014_2924">&nbsp;</a>SYNOPSIS</h4><blockquote>
<pre><code>

xargs <b>[</b>-t<b>][</b>-p<b>][</b>-e<b>[</b><i>eofstr</i><b>]][</b>-E <i>eofstr</i><b>][</b>-I <i>replstr</i><b>][</b>-i<b>[</b><i>replstr</i><b>]]
[</b>-L <i>number</i><b>][</b>-l<b>[</b><i>number</i><b>]][</b>-n <i>number </i><b>[</b>-x<b>]][</b>-s <i>size</i><b>][</b><i>utility </i><b>[</b><i>argument</i>...<b>]]
</b></code>
</pre>
</blockquote><h4><a name = "tag_001_014_2925">&nbsp;</a>DESCRIPTION</h4><blockquote>
The
<i>xargs</i>
utility constructs a command line consisting of the
<i>utility</i>
and
<i>argument</i>
operands specified followed by as many
arguments read in sequence from standard input as will fit in
length and number constraints specified by the options.
The
<i>xargs</i>
utility then invokes the constructed command line and
waits for its completion.
This sequence is repeated until an end-of-file
condition is detected on standard input or an invocation
of a constructed command line returns
an exit status of 255.
<p>
Arguments in the standard input must be separated by unquoted
blank characters,
or unescaped
blank characters
or
newline characters.
A string of zero or more non-double-quote
( )
and
non-newline
characters can be quoted by enclosing them in double-quotes.
A string of zero or more non-apostrophe
(')
and
non-newline
characters can be quoted by enclosing them in apostrophes.
Any unquoted character can be escaped by preceding it with a backslash.
The
<i>utility</i>
will be executed one or more times
until the end-of-file is reached.
The results are unspecified if the utility named by
<i>utility</i>
attempts to read from its standard input.
<p>
The generated command line length will be the sum of the size in bytes of
the utility name and each argument treated as strings, including a null
byte terminator for each of these strings.
The
<i>xargs</i>
utility will limit the command line length such that
when the command line is invoked, the combined argument and
environment lists (see the
<i>exec</i>
family of functions in the <b>XSH</b> specification)
will not exceed
{ARG_MAX}-2048
bytes.
Within this constraint, if neither the
<b>-n</b>
nor the
<b>-s</b>
option is specified,
the default command line length will be at least
{LINE_MAX}.
</blockquote><h4><a name = "tag_001_014_2926">&nbsp;</a>OPTIONS</h4><blockquote>
The
<i>xargs</i>
utility supports the <b>XBD</b> specification, <a href="../xbd/utilconv.html#usg"><b>Utility Syntax Guidelines</b>&nbsp;</a> ,
except that the
<b>-e</b>,
<b>-i</b>
and
<b>-l</b>
take optional option-arguments that cannot be separate arguments.
<p>
The following options are supported:
<dl compact>

<dt><b>-e[</b><i>eofstr</i><b>]</b><dd>
Use
<i>eofstr</i>
as the logical end-of-file string.
Underscore
(_)
is assumed for the logical EOF string if neither
<b>-e</b>
nor
<b>-E</b>
is used.
When the
<b>-eofstr</b>
option-argument is omitted, the logical EOF string
capability is disabled and underscores are taken literally.
The
<i>xargs</i>
utility
reads standard input until either end-of-file or
the logical EOF string is encountered.

<dt><b>-E&nbsp;</b><i>eofstr</i>
<dd>
Specify a logical end-of-file string to replace the default underscore.
The
<i>xargs</i>
utility
reads standard input until either end-of-file or
the logical EOF string is encountered.

<dt><b>-I&nbsp;</b><i>replstr</i>
<dd>
Insert mode:
<i>utility</i>
will be executed for each line from standard input,
taking the entire line as a single argument, inserting it in
<i>argument</i>s
for each occurrence of
<i>replstr</i>.
A maximum of five arguments in
<i>argument</i>s
can each contain one or more instances of
<i>replstr</i>.
Any
blank characters
at the beginning of each line are ignored.
Constructed arguments cannot grow larger than
255 bytes.
Option
<b>-x</b>
is forced on.
The
<b>-I</b>
and
<b>-i</b>
options are mutually exclusive; the last one specified
takes effect.


<dt><b>-i[</b><i>replstr</i><b>]</b><dd>
This option is equivalent to
<b>-I</b>&nbsp;<i>replstr</i>.
The string <b>{}</b> is assumed for
<i>replstr</i>
if the option-argument is omitted.

<dt><b>-L&nbsp;</b><i>number</i>
<dd>
The
<i>utility</i>
will be executed for each non-empty
<i>number</i>
lines of arguments from standard input.
The last invocation of
<i>utility</i>
will be with fewer lines of arguments if fewer than
<i>number</i>
remain.
A line is considered to end with the first
newline character
unless the last character of the line is a
blank character;
a trailing
blank character
signals continuation to the next non-empty line, inclusive.
The
<b>-L</b>,
<b>-l</b>
and
<b>-n</b>
options are mutually exclusive; the last one specified
takes effect.

<dt><b>-l[</b><i>number</i><b>]</b><dd>
(The letter ell.)
This option is equivalent to
<b>-L</b>&nbsp;<i>number</i>.
If
<i>number</i>
is omitted, 1 is assumed.
Option
<b>-x</b>
is forced on.

<dt><b>-n&nbsp;</b><i>number</i>
<dd>
Invoke
<i>utility</i>
using as many standard input arguments as possible, up to
<i>number</i>
(a positive decimal integer)
arguments maximum.
Fewer arguments will be used if:
<ul>

<li>
The command line length accumulated exceeds the size specified
by the
<b>-s</b>
option (or
{LINE_MAX}
if there is no
<b>-s</b>
option).

<li>
The last iteration has fewer than
<i>number</i>,
but not zero, operands remaining.

</ul>

<dt><b>-p</b>
<dd>Prompt mode: the user is asked whether to execute
<i>utility</i>
at each invocation.
Trace mode
(<b>-t</b>)
is turned on to write the command instance to be executed,
followed by a prompt to standard error.
An affirmative response read from
<b>/dev/tty</b>
will execute the command; otherwise,
that particular invocation of
<i>utility</i>
is skipped.

<dt><b>-s&nbsp;</b><i>size</i>
<dd>Invoke
<i>utility</i>
using as many standard input arguments as possible yielding a
command line length less than
<i>size</i>
(a positive decimal integer)
bytes.
Fewer arguments will be used if:
<ul>

<li>
The total number of arguments exceeds that specified by the
<b>-n</b>
option.

<li>
The total number of lines exceeds that specified by the
<b>-L</b>
option.

<li>
End-of-file is encountered on standard input before
<i>size</i>
bytes are accumulated.

</ul>

Values of
<i>size</i>
up to at least
{LINE_MAX}
bytes are supported,
provided that the constraints specified in the DESCRIPTION section
are met.
It is not considered an error if a value
larger than that supported by the implementation or exceeding
the constraints specified in the DESCRIPTION section is given;
<i>xargs</i>
will use the largest value it supports within the constraints.

<dt><b>-t</b>
<dd>Enable trace mode.
Each generated command line will be written to standard error
just prior to invocation.

<dt><b>-x</b>
<dd>Terminate if a command line containing
<i>number</i>
arguments (see the
<b>-n</b>
option above)
or
<i>number</i>
lines (see the
<b>-L</b>
option above)
will not fit in the implied or specified size (see the
<b>-s</b>
option above).

</dl>
</blockquote><h4><a name = "tag_001_014_2927">&nbsp;</a>OPERANDS</h4><blockquote>
The following operands are supported:
<dl compact>

<dt><i>utility</i><dd>The name of the utility to be invoked,
found by search path using the
<i>PATH</i>
environment variable, described in
the <b>XBD</b> specification, <a href="../xbd/envvar.html"><b>Environment Variables</b>&nbsp;</a> .
If
<i>utility</i>
is omitted,
the default is the
<i><a href="echo.html">echo</a></i>
utility.
If the
<i>utility</i>
operand names any of the special built-in utilities in
<xref href=sbi><a href="chap2.html#tag_001_014">
Special Built-in Utilities
</a></xref>,
the results are undefined.

<dt><i>argument</i><dd>
An initial option or operand for the invocation of
<i>utility</i>.

</dl>
</blockquote><h4><a name = "tag_001_014_2928">&nbsp;</a>STDIN</h4><blockquote>
The standard input must be a text file.
The results are unspecified if an end-of-file
condition is detected immediately following an escaped
newline character.
</blockquote><h4><a name = "tag_001_014_2929">&nbsp;</a>INPUT FILES</h4><blockquote>
The file
<b>/dev/tty</b>
is used to read responses required by the
<b>-p</b>
option.
</blockquote><h4><a name = "tag_001_014_2930">&nbsp;</a>ENVIRONMENT VARIABLES</h4><blockquote>
The following environment variables affect the execution of
<i>xargs</i>:
<dl compact>

<dt><i>LANG</i><dd>Provide a default value for the internationalisation variables
that are unset or null.
If
<i>LANG</i>
is unset or null, the corresponding value from the
implementation-dependent default locale will be used.
If any of the internationalisation variables contains an invalid setting, the
utility will behave as if none of the variables had been defined.

<dt><i>LC_ALL</i><dd>
If set to a non-empty string value,
override the values of all the other internationalisation variables.

<dt><i>LC_COLLATE</i><dd>
Determine the locale for the
behaviour of ranges, equivalence classes
and multi-character collating elements
used in the
extended regular expression defined for the
<b>yesexpr</b>
locale keyword in the LC_MESSAGES category.

<dt><i>LC_CTYPE</i><dd>
Determine the
locale for the interpretation of sequences of bytes of text data as
characters (for example, single- as opposed to multi-byte characters
in arguments and input files) and
the behaviour of character classes
used in the extended regular expression defined for the
<b>yesexpr</b>
locale keyword in the LC_MESSAGES category.

<dt><i>LC_MESSAGES</i><dd>
Determine the locale for the processing of affirmative responses
and that should be used to affect
the format and contents of diagnostic
messages written to standard error.

<dt><i>NLSPATH</i><dd>
Determine the location of message catalogues
for the processing of
<i>LC_MESSAGES .
</i>
<dt><i>PATH</i><dd>Determine the location of
<i>utility</i>,
as described in
the <b>XBD</b> specification, <a href="../xbd/envvar.html"><b>Environment Variables</b>&nbsp;</a> .

</dl>
</blockquote><h4><a name = "tag_001_014_2931">&nbsp;</a>ASYNCHRONOUS EVENTS</h4><blockquote>
Default.
</blockquote><h4><a name = "tag_001_014_2932">&nbsp;</a>STDOUT</h4><blockquote>
Not used.
<br>
</blockquote><h4><a name = "tag_001_014_2933">&nbsp;</a>STDERR</h4><blockquote>
Used for diagnostic messages and the
<b>-t</b>
and
<b>-p</b>
options.
If the
<b>-t</b>
option is specified, the
<i>utility</i>
and its constructed argument list will be written to standard error,
as it will be
invoked, prior to invocation.
If
<b>-p</b>
is specified, a prompt of the following format
will be written (in the POSIX locale):
<pre>
<code>
"?..."
</code>
</pre>
at the end of the line of the output from
<b>-t</b>.
</blockquote><h4><a name = "tag_001_014_2934">&nbsp;</a>OUTPUT FILES</h4><blockquote>
None.
</blockquote><h4><a name = "tag_001_014_2935">&nbsp;</a>EXTENDED DESCRIPTION</h4><blockquote>
None.
</blockquote><h4><a name = "tag_001_014_2936">&nbsp;</a>EXIT STATUS</h4><blockquote>
The following exit values are returned:
<dl compact>

<dt>0<dd>All invocations of
<i>utility</i>
returned exit status zero.

<dt>1-125<dd>A command line meeting the specified requirements could
not be assembled, one or more of the
invocations of
<i>utility</i>
returned a non-zero exit status,
or some other error occurred.

<dt>126<dd>The utility specified by
<i>utility</i>
was found but could not be invoked.

<dt>127<dd>The utility specified by
<i>utility</i>
could not be found.

</dl>
</blockquote><h4><a name = "tag_001_014_2937">&nbsp;</a>CONSEQUENCES OF ERRORS</h4><blockquote>
If a command line meeting the specified requirements cannot be
assembled, the utility cannot be invoked, an invocation of the
utility is terminated by a signal, or an invocation of the
utility exits with exit status 255, the
<i>xargs</i>
utility
will write a diagnostic message and exit without processing any
remaining input.
</blockquote><h4><a name = "tag_001_014_2938">&nbsp;</a>APPLICATION USAGE</h4><blockquote>
The 255 exit status (described as -1 in Issue 3)
allows a utility being used by
<i>xargs</i>
to tell
<i>xargs</i>
to terminate if it knows no further invocations using the
current data stream will succeed.
Thus,
<i>utility</i>
should explicitly
<i><a href="chap2.html#tag_001_014_007">exit</a></i>
with an appropriate value to avoid accidentally returning with 255.
<p>
Note that input is parsed as lines; blank characters separate arguments.
If
<i>xargs</i>
is used to bundle output of commands like
<i><a href="find.html">find</a></i>
<i>dir</i>
<b>-print</b>
or
<i><a href="ls.html">ls</a></i>
into commands to be executed,
unexpected results are likely if any filenames contain any
blank characters
or
newline characters.
This can be fixed by using
<i><a href="find.html">find</a></i>
to call a script that converts
each file found into a quoted string that is then piped to
<i>xargs</i>.
Note that the quoting rules used by
<i>xargs</i>
are not the same as in the shell.
They were not made consistent here
because existing applications depend on the current rules and
the shell syntax is not fully compatible with it.
An easy rule that can be used to transform any string into a quoted
form that
<i>xargs</i>
will interpret correctly is to precede each
character in the string with a backslash.
<p>
On implementations with a large value for
{ARG_MAX},
<i>xargs</i>
may produce command lines longer than
{LINE_MAX}.
For invocation of utilities, this is not a problem.
If
<i>xargs</i>
is being used
to create a text file, users should explicitly set the maximum
command line length with the
<b>-s</b>
option.
<p>
The
<i><a href="command.html">command</a></i>,
<i><a href="env.html">env</a></i>,
<i><a href="nice.html">nice</a></i>,
<i><a href="nohup.html">nohup</a></i>,
<i><a href="time.html">time</a></i>
and
<i>xargs</i>
utilities have been specified to use
exit code 127 if an error occurs so that
applications can distinguish
&quot;failure to find a utility&quot; from &quot;invoked utility exited
with an error indication.&quot;
The value 127 was chosen because it is not commonly used for other meanings;
most utilities use small values for &quot;normal error conditions&quot; and
the values above 128 can be confused with termination due to receipt of a
signal.
The value 126
was chosen in a similar manner to indicate that the utility
could be found, but not invoked.
Some scripts produce meaningful error messages
differentiating the 126 and 127 cases.
The distinction between exit codes 126 and 127 is based
on KornShell practice that uses 127 when all attempts to
<i>exec</i>
the utility fail with
[ENOENT],
and uses 126 when any attempt to
<i>exec</i>
the utility fails for any other reason.
<br>
</blockquote><h4><a name = "tag_001_014_2939">&nbsp;</a>EXAMPLES</h4><blockquote>
<ol>
<p>
<li>
The following will move all files from directory
$1
to directory
$2,
and echo each move command just before doing it:
<pre>
<code>
ls $1 | xargs -I {} -t mv $1/{} $2/{}
</code>
</pre>
<p>
<li>
The following command
will combine the output of the parenthesised commands onto one line,
which is then written to the end-of-file
<b>log</b>:
<pre>
<code>
(logname; date; printf "%s\n" "$0 $*") | xargs &gt;&gt;log
</code>
</pre>
<p>
<li>
The following command
will invoke
<i><a href="diff.html">diff</a></i>
with successive
pairs of arguments originally typed as command line arguments
(assuming there are no embedded
blank characters
in the elements
of the original argument list):
<pre>
<code>
printf "%s\n" "$*" | xargs -n 2 -x diff
</code>
</pre>
<p>
<li>
The user is asked which files in the current directory are to be
archived.
The files are archived into
<b>arch</b>;
a, one at a time, or b, many at a time.
<pre>
<code>
a.&nbsp;&nbsp;ls | xargs -p -L 1 ar -r arch

b.&nbsp;&nbsp;ls | xargs -p -L 1 | xargs ar -r arch
</code>
</pre>
<p>
<p>
<li>
The following will execute
with successive
pairs of arguments originally typed as command line arguments:
<pre>
<code>
echo $* | xargs -n 2 diff
</code>
</pre>
<p>
</ol>
</blockquote><h4><a name = "tag_001_014_2940">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>
A version supporting
the Utility Syntax Guidelines may be introduced.
<p>
The IEEE PASC 1003.2 Interpretations Committee has forwarded concerns about
parts of this interface definition to the IEEE PASC Shell and Utilities Working Group
which is identifying the corrections.
A future revision of this specification will align with
IEEE Std. 1003.2b when finalised.
</blockquote><h4><a name = "tag_001_014_2941">&nbsp;</a>SEE ALSO</h4><blockquote>
<i><a href="echo.html">echo</a></i>.
</blockquote><hr size=2 noshade>
<center><font size=2>
UNIX &reg; is a registered Trademark of The Open Group.<br>
Copyright &copy; 1997 The Open Group
<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]

</font></center><hr size=2 noshade>
</body></html>
